package com.personal.common.util;

import java.util.Stack;

public class ScaleConvert32Util {
	 private static char[] charSet = "0123456789abcdefghijklmnopqrstuvwxyz"
				.toCharArray();

		/**
		 * 将10进制转化为62进制
		 * 
		 * @param number
		 * @param length
		 *            转化成的62进制长度，不足length长度的话高位补0，否则不改变什么
		 * @return
		 */
		public static String convert_10_to_32(long number, int length) {
			Long rest = number;
			Stack<Character> stack = new Stack<Character>();
			StringBuilder result = new StringBuilder(0);
			while (rest != 0) {
				stack.add(charSet[new Long((rest - (rest / 36) * 36)).intValue()]);
				rest = rest / 36;
			}
			for (; !stack.isEmpty();) {
				result.append(stack.pop());
			}
			long result_length = result.length();
			StringBuilder temp0 = new StringBuilder();
			for (long i = 0; i < length - result_length; i++) {
				temp0.append('0');
			}

			return temp0.toString() + result.toString();

		}

		/**
		 * 将62进制转换成10进制数
		 * 
		 * @param ident62
		 * @return
		 */
		public static String convert_32_to_10(String ident62) {
			long decimal = 0;
			long base = 36;
			long keisu = 0;
			long cnt = 0;

			byte ident[] = ident62.getBytes();
			for (int i = ident.length - 1; i >= 0; i--) {
				int num = 0;
				if (ident[i] > 48 && ident[i] <= 57) {
					num = ident[i] - 48;
				} else if (ident[i] >= 65 && ident[i] <= 90) {
					num = ident[i] - 65 + 10;
				} else if (ident[i] >= 97 && ident[i] <= 122) {
					num = ident[i] - 97 + 10;
				}
				keisu = (int) java.lang.Math.pow((double) base, (double) cnt);
				decimal += num * keisu;
				cnt++;
			}
			// return String.format( "%08d", decimal );
			return decimal + "";
		}

	 public static void main(String[] args) {
	 /* System.out.println(ScaleConvert32.numericToString(548454, 32));
	  System.out.println(ScaleConvert32.stringToNumeric("57c02", 32));
	  System.out.println(ScaleConvert32.stringToNumeric("gpk6", 32));
	  
	  
	  System.out.println(ScaleConvert32.toCustomNumericString(1, 32));
	  System.out.println(ScaleConvert32.toCustomNumeric("00001", 32));
	  
	  String numStr = ScaleConvert32.toCustomNumericString(111, 32);
	  int five = 5; 
	  if(numStr.length()<five){
		  int addLength = five - numStr.length();
		  for(int i = 0 ;i< addLength;i++){
			  numStr = "0"+numStr;
		  }
	  }
	  System.out.println(numStr+"=================");
	  System.out.println(ScaleConvert32.toCustomNumericString(32722737, 32));
	  System.out.println(ScaleConvert32.toCustomNumeric("t9ilh", 32));
	  System.out.println(ScaleConvert32.toCustomNumeric("v6jph", 32));*/
		 
		 
		 System.out.println(convert_10_to_32(9,3));
		 System.out.println(convert_32_to_10("00a"));
		 
		 System.out.println(convert_10_to_32(163,3));
		 System.out.println(convert_32_to_10("334"));
		 
		 System.out.println(convert_10_to_32(46655,3));//3位能转换的最大十进制数
		 System.out.println(convert_32_to_10("zzz"));
	 }
}
